/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.diamondsys.embrapa;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.Root;

/**
 *
 * @author Pablo
 */
@Stateless
public class SubcategoriaFacade extends AbstractFacade<Subcategoria> {
    @PersistenceContext(unitName = "com.diamondsys_embrapa_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public SubcategoriaFacade() {
        super(Subcategoria.class);
    }
    
    public void removeItem(int id){
        
        CriteriaBuilder cb = em.getCriteriaBuilder();

        // create delete
        CriteriaDelete<Subcategoria> delete = cb.
           createCriteriaDelete(Subcategoria.class);

        // set the root class
        Root e = delete.from(Subcategoria.class);

        // set where clause
        delete.where(cb.equal(e.get("id"), id));

        // perform update
        em.createQuery(delete).executeUpdate();
    }
    
    @Override
    public List<Subcategoria> findAll() {
        return em.createQuery("SELECT s FROM Subcategoria s WHERE s.descricao != '{Nenhuma}' ORDER BY s.descricao", Subcategoria.class).getResultList();
    }    

    public List<Subcategoria> findAllByCategoria(Categoria cat) {
        return em.createQuery("SELECT s FROM Subcategoria s WHERE s.categoriaId = :inCategoriaId ORDER BY s.descricao", Subcategoria.class)
                .setParameter("inCategoriaId", cat)
                .getResultList();
    }       
    
}
